<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('hostjogja');
        $this->load->library('pagination');
        $this->load->library('parser');
        $log = $this->session->all_userdata();
        if ($log['login'] == NULL && $log['privilege'] == NULL) {
            redirect('main/logout');
        }
    }

    function trueLogin() { //cek sudah login
        $log = $this->session->all_userdata();
        if ($log['login'] == NULL && $log['privilege'] == NULL) {
            redirect('main/logout');
        }
    }

    function trueAdmin() {
        $log = $this->session->all_userdata();
        if ($log['privilege'] != 1) {
            redirect('admin/error');
        }
    }

    function trueAdminStaff() {
        $log = $this->session->all_userdata();
        if ($log['privilege'] == 2) {
            redirect('admin/error');
        }
    }

    function trueAdminInves() {
        $log = $this->session->all_userdata();
        if ($log['privilege'] == 3) {
            redirect('admin/error');
        }
    }

    function notfound() {
        $this->trueLogin();
        $data['blog_title'] = '404 Page Not Found';
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/template/notfound');
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function error() {
        $this->trueLogin();
        $data['blog_title'] = 'Error page';
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/template/error');
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function index() {
        $this->trueLogin();

        redirect('admin/home');
    }

    function home() {
        $this->trueLogin();
        $data['blog_title'] = 'Home';
        $id = $this->session->all_userdata();
        $data['nama'] = $this->hostjogja->nama($id);
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/home', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function produk() {
        $this->trueLogin();
        $this->trueAdminInves();
        $data['blog_title'] = 'Produk';
        $data['domain'] = $this->hostjogja->domain();
        $data['hosting'] = $this->hostjogja->hosting();
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/produk', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function omset() {
        $this->trueLogin();
        $data['blog_title'] = 'Pemasukan';
        $data['sell_domain'] = $this->hostjogja->sell_domain();
        $data['total_sell_domain'] = $this->hostjogja->total_domain();
        $data['sell_hosting'] = $this->hostjogja->sell_hosting();
        $data['total_sell_hosting'] = $this->hostjogja->total_hosting();
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/omset', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function customer() {
        $this->trueLogin();
        $data['blog_title'] = 'Customer';
        $data['customer'] = $this->hostjogja->customer();
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/customer', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function addCustomer() {
        $this->trueLogin();
        $data['blog_title'] = 'Customer Add';
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/addCustomer', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function addCus() {
        $this->trueLogin();
        $this->trueAdminStaff();
        $data['nama'] = $this->input->post('nama');
        $data['username'] = $this->session->userdata('username');
        $this->hostjogja->addCus($data);
        redirect('admin/customer');
    }

    function transaksi() {
        $this->trueLogin();
        $data['blog_title'] = 'Pemasukan';
        $data['sell_all'] = $this->hostjogja->sell_all();
        $data['grand'] = $this->hostjogja->sell_all_total();
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/transaksi', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function addTransaksi() {
        $this->trueLogin();
        $this->trueAdminStaff();
        $data['blog_title'] = 'Tambah Transaksi';
        $data['customer'] = $this->hostjogja->customer();
        $data['jenis'] = $this->hostjogja->jenis();
        $data['id'] = $this->hostjogja->nextId();
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/addTransaksi', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function addTrans() {
        $this->trueLogin();
        $this->trueAdminStaff();
        $data['id'] = $this->input->post('id');
        $data['date'] = $this->input->post('date');
        $data['str'] = $this->input->post('start');
        $data['end'] = $this->input->post('end');
        $data['customer_id'] = $this->input->post('cus');
        $data['jenis_id'] = $this->input->post('jenis');
        $data['qty'] = $this->input->post('qty');
        $data['disc'] = $this->input->post('disc');
        $data['sts'] = $this->input->post('status');
        $data['username'] = $this->session->userdata('username');
        $this->hostjogja->addInvoice($data);
        $this->hostjogja->addDetailInvoice($data);
        redirect('admin/transaksi');
    }

    function addDetailTransaksi() {
        $this->trueLogin();
        $data['blog_title'] = 'Tambah Detail Transaksi';
        $data['customer'] = $this->hostjogja->customer();
        $data['jenis'] = $this->hostjogja->jenis();
        $data['id'] = $this->hostjogja->nextId();
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/addDetailTransaksi', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function addDetTrans() {
        $this->trueLogin();
        $this->trueAdminStaff();
        $data['id'] = $this->input->post('id');
        $data['date'] = $this->input->post('date');
        $data['str'] = $this->input->post('start');
        $data['end'] = $this->input->post('end');
        $data['customer_id'] = $this->input->post('cus');
        $data['jenis_id'] = $this->input->post('jenis');
        $data['qty'] = $this->input->post('qty');
        $data['disc'] = $this->input->post('disc');
        $data['sts'] = $this->input->post('status');
        $data['username'] = $this->session->userdata('username');
        $this->hostjogja->addDetailInvoice($data);
        redirect('admin/transaksi');
    }

    function pengeluaran() {
        $this->trueLogin();
        $this->trueAdminInves();
        $data['blog_title'] = 'Pengeluaran';
        $data['pengeluaran'] = $this->hostjogja->pengeluaran();
        $data['total'] = $this->hostjogja->totalPengeluaran();
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/pengeluaran', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function addPengeluaran() {
        $this->trueLogin();
        $data['blog_title'] = 'Tambah Pengeluaran';
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/addPengeluaran', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function addDetailPengeluaran() {
        $this->trueLogin();
        $data['blog_title'] = 'Tambah Detail Pengeluaran';
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/addDetailPengeluaran', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function addNota() {
        $this->trueLogin();
        $this->trueAdminStaff();
        $input['no'] = $this->input->post('id');
        $input['tgl'] = $this->input->post('date');
        $input['petugas'] = $this->session->userdata('username');
        $input['qty'] = $this->input->post('qty');
        $input['harga'] = $this->input->post('harga');
        $input['detail'] = $this->input->post('detail');
        $this->hostjogja->addNota($input);
        $this->hostjogja->addDetailNota($input);
        redirect('admin/pengeluaran');
    }

    function addDetail() {
        $this->trueLogin();
        $this->trueAdminStaff();
        $input['no'] = $this->input->post('id');
        $input['qty'] = $this->input->post('qty');
        $input['harga'] = $this->input->post('harga');
        $input['detail'] = $this->input->post('detail');
        $this->hostjogja->addDetailNota($input);
        redirect('admin/pengeluaran');
    }

    function keuntungan() {
        $this->trueLogin();
        $this->trueAdminInves();
        $data['blog_title'] = 'Daftar Keuntungan';
        $data['keuntungan'] = $this->hostjogja->keuntungan();
        $data['total'] = $this->hostjogja->totalKeuntungan();
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/keuntungan', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function keuntunganGroup() {
        $this->trueLogin();
        $this->trueAdmin();
        $data['blog_title'] = 'Keuntungan Group';
        $data['keuntungan'] = $this->hostjogja->keuntunganGroup();
        $data['total'] = $this->hostjogja->totalKeuntunganGroup();
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/keuntunganGroup', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

    function bagiHasil() {
        $this->trueLogin();
        $this->trueAdminInves();
        $data['blog_title'] = 'Bagi Hasil';
        $data['keuntungan'] = $this->hostjogja->totalKeuntungan();
        $this->parser->parse('admin/template/header', $data);
        $this->load->view('admin/template/open_container');
        $this->load->view('admin/bagiHasil', $data);
        $this->load->view('admin/template/close_container');
        $this->load->view('admin/template/footer');
    }

}

?>
